package com.hz.ruoyimqttconsume.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    // 普通队列（用于及时发送信息）
    @Bean
    public Queue alertQueue() {
        return new Queue("alert.queue", true);
    }

    // 普通交换机
    @Bean
    public DirectExchange alertExchange() {
        return new DirectExchange("alert.exchange");
    }

    // 绑定普通队列和交换机
    @Bean
    public Binding alertBinding() {
        return BindingBuilder.bind(alertQueue()).to(alertExchange()).with("alert.queue");
    }

    // 延时队列（用于延时记录）
    @Bean
    public Queue delayQueue() {
        return QueueBuilder.durable("delay.queue")
                .withArgument("x-dead-letter-exchange", "dlx.exchange") // 死信交换机
                .withArgument("x-dead-letter-routing-key", "dlx.queue")  // 死信路由键
                .build();
    }

    // 延时交换机
    @Bean
    public DirectExchange delayExchange() {
        return new DirectExchange("delay.exchange");
    }

    // 绑定延时队列和交换机
    @Bean
    public Binding delayBinding() {
        return BindingBuilder.bind(delayQueue()).to(delayExchange()).with("delay.queue");
    }

    // 死信队列
    @Bean
    public Queue dlxQueue() {
        return new Queue("dlx.queue", true);
    }

    // 死信交换机
    @Bean
    public DirectExchange dlxExchange() {
        return new DirectExchange("dlx.exchange");
    }

    // 绑定死信队列和交换机
    @Bean
    public Binding dlxBinding() {
        return BindingBuilder.bind(dlxQueue()).to(dlxExchange()).with("dlx.queue");
    }
}